﻿using Oplug.Services;
using System.Linq;
using System.Windows.Forms;

namespace Oplug.CoolTower.Filters
{
    public class MethodFilters
    {
        public static bool IsDeny(string methodName, string formName, string conn)
        {
            try
            {
                using (PowerService ObjServ = new PowerService(conn))
                {
                    var obj = ObjServ.formMethods.GetAllList(o => o.FormName == formName && o.MethodName == methodName).FirstOrDefault();
                    if (obj != null)
                    {
                        var arr = obj.DenyUsers.Split(',');
                        bool result = arr.Contains(Utilities.UserID);       
                        if(result)
                        {
                            ShowWaring(methodName);
                        }
                        return result;
                    }
                    else
                        return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool IsAllow(string methodName, string formName, string conn)
        {
            try
            {
                using (PowerService ObjServ = new PowerService(conn))
                {
                    var obj = ObjServ.formMethods.GetAllList(o => o.FormName == formName && o.MethodName == methodName).FirstOrDefault();
                    if (obj != null)
                    {
                        var arr = obj.AllowUsers.Split(',');
                        bool result = arr.Contains(Utilities.UserID);
                        if (!result)
                        {
                            ShowWaring(methodName);
                        }
                        return result;
                    }
                    else
                        return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public static void ShowWaring(string methodName) {
            MessageBox.Show("您没有执行" + methodName + "的权限！请联系管理员。", "系统", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

}
